package com.yiji.fintech.musfinance.common.dispatcher.base;

import com.acooly.core.common.exception.OrderCheckException;
import com.acooly.core.utils.validate.Validators;
import com.yiji.fintech.musfinance.common.annotation.LogAspectAnnotation;
import com.yiji.fintech.musfinance.center.base.order.OrderFacade;
import com.yiji.fintech.musfinance.center.base.result.ResultFacade;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

/**
 * Created by ouwen@yiji.com} on 2017/9/13.
 */
@Slf4j
public abstract class BaseDispatcher {
    /**
     * 消息分发
     * @param orderFacade
     * @param result
     */
    protected abstract void dispatch(OrderFacade orderFacade, ResultFacade result);
    @LogAspectAnnotation("dispatch")
    public ResultFacade doExcute(OrderFacade orderFacade, ResultFacade result){
        dispatch(orderFacade,result);
        return result;
    }
    protected void checkOrder(OrderFacade order){
        if (null == order) {
            throw new OrderCheckException();
        }
        try {
            Validators.assertJSR303(order);
            order.check();
        } catch (IllegalArgumentException iae) {
            throw new OrderCheckException(iae.getMessage(), "请求参数不完整:");
        } catch (Exception e) {
            throw new OrderCheckException(StringUtils.left(e.getMessage(), 200),"公共参数合法性检查未通过:");
        }
    }
}
